package me.keiwu.user.controller;

import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.user.domain.Medal;
import me.keiwu.user.domain.User;
import me.keiwu.user.service.MedalService;
import me.keiwu.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by kei on 30/01/2018.
 */
@RestController
@RequestMapping("/medal")
public class MedalController {

    @Autowired
    private MedalService medalService;
    @Autowired
    private UserService userService;

    @RequestMapping("list")
    public ResponseV2 list(HttpServletRequest request,
                           @RequestParam(defaultValue = "0") Integer page,
                           @RequestParam(defaultValue = "10") Integer size) {
        Long userId = ContextUtil.getLoginUserId(request);
        Page<Medal> list = medalService.findAllMedals(userId, page, size);
        return ResponseV2.ok(list);
    }

    @RequestMapping("exchange")
    public ResponseV2 exchange(HttpServletRequest request, Integer medalId) {
        Long userId = ContextUtil.getLoginUserId(request);
        return medalService.exchangeMedal(userId, medalId);
    }

    @RequestMapping("display")
    public ResponseV2 display(HttpServletRequest request, Integer medalId, boolean display) {
        Long userId = ContextUtil.getLoginUserId(request);
        return medalService.displayUserMedal(userId, medalId, display);
    }


    @RequestMapping("present/contact")
    public ResponseV2 presentContact(HttpServletRequest request, Integer medalId, String name, String telephone,
                                 String address, String zip) {
        Long userId = ContextUtil.getLoginUserId(request);
        User user = userService.getUserById(userId);
        return medalService.medalPresentContact(userId, user.getNickName(), medalId, name, telephone, address, zip);
    }

}
